package com.blacktry.controller;

import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.service.JobService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.entity.schedule.ScheduleJob;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @calssName ScheduleController
 * @Description TODO
 * @Author blackTry
 */
@Api(value="定时任务", tags = "定时任务管理接口")
@RestController
@RequestMapping("/schedule")
public class JobController {

    @Autowired
    private JobService jobService;

    /**
     * 查看所有任务列表
     */
    @OperLog(operModul = "定时任务-添加定时任务",operType = OprLogConst.ADD,operDesc = "添加定时任务")
    @ApiOperation("添加定时任务")
    @PostMapping("/addJob")
    public ApiResult<ScheduleJob> addJob(@RequestBody ScheduleJob scheduleJob){
        try{
            return ApiResultHandler.buildApiResult(200, "成功",jobService.addJob(scheduleJob));
        }catch (Exception e){
            return ApiResultHandler.buildApiResult(-1,"查询失败！");
        }
    }

    /**
     * 查看所有任务列表
     */
    @OperLog(operModul = "定时任务-查看所有任务列表",operType = OprLogConst.SELECT,operDesc = "查看所有任务列表")
    @ApiOperation("查看所有任务列表")
    @GetMapping("/list")
    public ApiResult getJobList(){
        try{
            Map<String, Object> result = jobService.getAllJob();
            return ApiResultHandler.buildApiResult(200, "成功",result);
        }catch (Exception e){
            return ApiResultHandler.buildApiResult(-1,"查询失败！");
        }
    }

    /**
     * 查看某个任务的执行详情
     */
    @OperLog(operModul = "定时任务-查看某个任务的执行详情",operType = OprLogConst.SELECT,operDesc = "查看某个任务的执行详情")
    @ApiOperation("查看某个任务的执行详情")
    @GetMapping("/schedule/detail")
    public ApiResult getJobScheduleDetail(@RequestParam("jobId") int jobId){
        try{
            Map<String, Object> result = jobService.getJobScheduleDetail(jobId);
            return ApiResultHandler.buildApiResult(200, "成功",result);
        }catch (Exception e){
            return ApiResultHandler.buildApiResult(-1,"查询失败！");
        }
    }

    /**
     * 查看某个任务的详情
     */
    @OperLog(operModul = "定时任务-查看某个任务的详情",operType = OprLogConst.SELECT,operDesc = "查看某个任务的详情")
    @ApiOperation("查看某个任务的详情")
    @GetMapping("/detail")
    public ApiResult getJobDetail(@RequestParam("jobId") int jobId){
        try{
            Map<String, Object> result = jobService.getJobDetail(jobId);
            return ApiResultHandler.buildApiResult(200, "成功",result);
        }catch (Exception e){
            return ApiResultHandler.buildApiResult(-1,"查询失败！");
        }
    }

    /**
     * 修改任务
     */
    @OperLog(operModul = "定时任务-修改任务",operType = OprLogConst.UPDATE,operDesc = "修改任务")
    @ApiOperation("修改任务")
    @PostMapping("/update")
    public ApiResult updateJob(@RequestBody ScheduleJob job){
        try{
            Map<String, Object> result = jobService.updateJob(job);
            return ApiResultHandler.buildApiResult(200, "成功",result);
        }catch (Exception e){
            return ApiResultHandler.buildApiResult(-1,"查询失败！");
        }
    }
}